﻿myApp.factory('MuseumService', ['$http', '$q', function ($http, $q) {
    // GET all Items
    var getMuseums = function () {
        var defered = $q.defer();
        $http.get('api/museums').success(function (data) {
            defered.resolve(data);
            console.log(data)
        }).error(function(){
            defered.reject();
        })
        return defered.promise;
    }

    // GET all Items based on search criteria
    var getMuseumsSearch = function (str) {
        var defered = $q.defer();
        $http.get('api/museums/search/'+str).success(function (data) {
            defered.resolve(data);
            console.log(data)
        }).error(function () {
            defered.reject();
        })
        return defered.promise;
    }


    // Get specific Item
    var getMuseum = function (id) {
        var defered = $q.defer();
        $http.get('api/museums/Get/' + id).success(function (data) {
            defered.resolve(data);
            console.log(data)
        }).error(function () {
            defered.reject();
        })
        return defered.promise;
    }
    // Add a review to museum
    var addReview = function (rv) {
        var deferred = $q.defer();
        $http.post('api/museums/Post', rv).success(function (data, status, headers, config) {
            deferred.resolve();
        }).error(function (data, status, headers, config) {
            deferred.reject();
        });
        return deferred.promise;
    }
    // Delete a review by ID
    var deleteReview = function (rid) {
        var def = $q.defer();
        $http.delete('api/museums/deleteReview/' + rid).success(function (data, status, headers, config) {
            def.resolve();
        }).error(function (data, status, headers, config) {
            def.reject();
        });
        return def.promise;
    }
    
    var getWeather = function (str) {
        var defered = $q.defer();
        $http.get('http://api.openweathermap.org/data/2.5/weather?q='+str).success(function (data) {
            defered.resolve(data);
        }).error(function () {
            defered.reject();
        })
        return defered.promise;
    }


    // Public User methods
    return {
        getmus: getMuseums,
        getMuseumsSearch: getMuseumsSearch,
        getMuseum: getMuseum,
        addReview: addReview,
        deleteReview: deleteReview,
        getWeather: getWeather
    }

}])